package com.atguigu.guli.service.edu.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Chapter;
import com.atguigu.guli.service.edu.entity.vo.ChapterVo;
import com.atguigu.guli.service.edu.service.ChapterService;
import com.atguigu.guli.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2020-10-14
 */
@RestController

@RequestMapping("/admin/edu/chapter")
@Api(tags = "章节管理")
public class AdminChapterController {
    @Autowired
    private ChapterService chapterService;

    @Autowired(required = true)
    private VideoService videoService;

    @ApiOperation("章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(@PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.nestedListByCourseId(courseId);
        return R.ok().data("items",chapterVoList);
    }

    @ApiOperation("新增章节")
    @PostMapping("/save")
    public R saveChapter(@RequestBody Chapter chapter){
        boolean save = chapterService.save(chapter);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/remove/{id}")
    public R deleteChapter(@PathVariable String id){
        boolean b = chapterService.removeById(id);
        if (b){
            //删除视频：VOD
            videoService.removeMediaVideoByChapterId(id);
            return R.ok().message("删除成功");
        }else {
            return R.error().message("数据不存在");
        }

    }

    @ApiOperation("根据id查询章节")
    @GetMapping("/get/{id}")
    public R getById(
            @ApiParam(value="章节id", required = true)
            @PathVariable String id){

        Chapter chapter = chapterService.getById(id);
        if (chapter != null) {
            return R.ok().data("item", chapter);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("修改章节")
    @PutMapping("/update")
    public R updateChapter(@RequestBody Chapter chapter){
        boolean b = chapterService.updateById(chapter);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }

    }

}

